﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using ebMS3dotnet.Core;
using ebMS3dotnet.Core.Configuration;

namespace ebMS3dotnet.Core
{
    internal class ebMS3UserMessageWriter : IebMS3HeaderWriter
    {
        private ebMS3ClientParameters _clientParameters;
        public ebMS3UserMessageWriter(ebMS3ClientParameters clientParameters)
        {

            if (clientParameters == null)
                throw new ArgumentNullException("pMode", "PMode cannot be null");

            _clientParameters = clientParameters;
        }

        public void WriteHeader(XmlDictionaryWriter writer)
        {
            writer.WriteStartElement("UserMessage", Constants.EBMS3_NAMESPACE);
            if (!string.IsNullOrWhiteSpace(_clientParameters.UserMessage.Mpc))
                writer.WriteAttributeString("mpc", _clientParameters.UserMessage.Mpc);
            

            writer.WriteStartElement("MessageInfo", Constants.EBMS3_NAMESPACE);
            writer.WriteElementString("Timestamp", Constants.EBMS3_NAMESPACE, _clientParameters.UserMessage.MessageInfo.Timestamp.ToString("u"));
            writer.WriteElementString("MessageId", Constants.EBMS3_NAMESPACE, _clientParameters.UserMessage.MessageInfo.MessageID);
            if (!string.IsNullOrWhiteSpace(_clientParameters.UserMessage.MessageInfo.RefToMessageId))
                writer.WriteElementString("RefToMessageId", Constants.EBMS3_NAMESPACE, _clientParameters.UserMessage.MessageInfo.MessageID);
            //Message Info
            writer.WriteEndElement();

            writer.WriteStartElement("PartyInfo", Constants.EBMS3_NAMESPACE);
            foreach (var party in _clientParameters.UserMessage.PartyInfo.From)
            {
                writer.WriteStartElement("From", Constants.EBMS3_NAMESPACE);
                writer.WriteStartElement("PartyId", Constants.EBMS3_NAMESPACE);
                if (!string.IsNullOrWhiteSpace(party.Type))
                    writer.WriteAttributeString("type", party.Type);
                writer.WriteString(party.PartyId);
                writer.WriteEndElement();
               
                writer.WriteElementString("Role", Constants.EBMS3_NAMESPACE, party.Role);
                writer.WriteEndElement();
            }

            writer.WriteStartElement("To", Constants.EBMS3_NAMESPACE);
            writer.WriteStartElement("PartyId", Constants.EBMS3_NAMESPACE);
            if (!string.IsNullOrWhiteSpace(_clientParameters.UserMessage.PartyInfo.To.Type))
                writer.WriteAttributeString("type", _clientParameters.UserMessage.PartyInfo.To.Type);
            writer.WriteString(_clientParameters.UserMessage.PartyInfo.To.PartyId);
            writer.WriteEndElement();

            writer.WriteElementString("Role", Constants.EBMS3_NAMESPACE, _clientParameters.UserMessage.PartyInfo.To.Role);
            writer.WriteEndElement();       

            //Party Info
            writer.WriteEndElement();

            writer.WriteStartElement("CollaborationInfo", Constants.EBMS3_NAMESPACE);
            if(!string.IsNullOrWhiteSpace(_clientParameters.UserMessage.CollaborationInfo.AgreementRef.Value))
            {
                writer.WriteStartElement("AgreementRef",Constants.EBMS3_NAMESPACE);
                if (!string.IsNullOrWhiteSpace(_clientParameters.UserMessage.CollaborationInfo.AgreementRef.Type))
                    writer.WriteAttributeString("type", _clientParameters.UserMessage.CollaborationInfo.AgreementRef.Type);
                if (!string.IsNullOrWhiteSpace(_clientParameters.UserMessage.CollaborationInfo.AgreementRef.PMode))
                    writer.WriteAttributeString("pmode", _clientParameters.UserMessage.CollaborationInfo.AgreementRef.PMode);
                writer.WriteString(_clientParameters.UserMessage.CollaborationInfo.AgreementRef.Value);
                // Agreement Ref
                writer.WriteEndElement();
            }

            writer.WriteStartElement("Service", Constants.EBMS3_NAMESPACE);
            if (!string.IsNullOrWhiteSpace(_clientParameters.UserMessage.CollaborationInfo.Service.Type))
                writer.WriteAttributeString("type", _clientParameters.UserMessage.CollaborationInfo.Service.Type);
            writer.WriteString(_clientParameters.UserMessage.CollaborationInfo.Service.Value);
            //Service
            writer.WriteEndElement();

            writer.WriteElementString("Action", Constants.EBMS3_NAMESPACE, _clientParameters.UserMessage.CollaborationInfo.Action);

            writer.WriteElementString("ConversationId", Constants.EBMS3_NAMESPACE, _clientParameters.UserMessage.CollaborationInfo.ConversationID);

            //Collaboration Info
            writer.WriteEndElement();
            
            //User Message
            writer.WriteEndElement();
        }       
    }
}
